<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<!DOCTYPE html>
<html>

	<head>
		<meta  charset=UTF-8">
		<title>添加解决方案</title>
		<link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
		<!-- 引用Vue -->
		<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
		<!-- 引入组件库 -->
		<script src="https://unpkg.com/element-ui/lib/index.js"></script>
				
<script type="text/javascript" src="../js/jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="../js/axios.js"></script>
<script type="text/javascript" src="../js/qs.js"></script>
		<style>
			*{
			margin: 0;
			padding: 0;
		}
		.app{
			width: 780px;
			height:600px ;
			}
		
	</style>
	</head>
	<body>
		<div id="app" class="app">
			<el-form :model="form" :rules="rules" ref="form" label-width="100px" class="demo-ruleForm">

				<el-form-item label="客户" prop="custname">
					<el-col :span="22">
						<el-input v-model="form.custname" disabled></el-input>
					</el-col>
				</el-form-item>
				<el-form-item>
					<el-autocomplete class="inline-input" v-model="form.custname" :fetch-suggestions="querySearch" placeholder="请输入内容" :trigger-on-focus="false" @select="handleSelect">
												<template slot-scope="{ item }">
  														  <div class="name">{{ item.cus_name }}({{ item.cus_company}})</div>
												  </template>
												</el-autocomplete>
				</el-form-item>

				<el-form-item label="方案主题" prop="theme">
					<el-col :span="22">
						<el-input v-model="form.theme"></el-input>
					</el-col>
				</el-form-item>

				<el-form-item label="对应机会">
					<el-col :span="10">
						<el-form-item  prop="opp">
						<template>
							<el-select v-model="form.opp" placeholder="请选择" @change="cha">
								<el-option  v-for="item in opport":key="item.opp_ids":label="item.opp_name":value="item.opp_ids">
							</el-select>
						</template>
				       </el-form-item>
					</el-col>
					<el-col :span="9">
						<el-form-item label="提交时间" prop="sdate">
							<el-date-picker type="date" placeholder="选择日期" v-model="form.sdate" style="width: 100%;" format="yyyy-MM-dd" value-format="yyyy-MM-dd" :picker-options="pickerOptions2"></el-date-picker>
						</el-form-item>
					</el-col>
				</el-form-item>

				<el-form-item label="方案内容" prop="cont">
					<el-col :span="22">
						<el-input type="textarea"  placeholder="请输入内容" v-model="form.cont">
						</el-input>
					</el-col>
				</el-form-item>
				<el-form-item label="客户反馈" prop=fback"">
					<el-col :span="22">
						<el-input type="textarea"  placeholder="请输入内容" v-model="form.fback">
						</el-input>
					</el-col>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="submitForm('form')">立即创建</el-button>
					<el-button @click="resetForm('form')">重置</el-button>
				</el-form-item>

			</el-form>
		</div>
		<script>
			var vm = new Vue({
				el: "#app",
				data() {
					return {
						customertoman:[],
						 opport:[],
						 pickerOptions2: { //时间做约束
								disabledDate(time) {
									return time.getTime() < Date.now() - 8.64e7;
								}
							},
						form: {
							theme: '',
							custname: '',
							custid:'',
							solid:'',
							opp: '',
							sdate: '',
							cont: '',
							fback: '',
							oppid:'',
							restaurants: []
						},
						rules: { //数据源model
							theme: [{
								required: true,
								message: '请输入机会主题',
								trigger: 'blur'
							}],

							custname: [{

								required: true,
								message: '请选择客户',
								trigger: 'blur'
							}],
							
							sdate: [
								//数据类型要是date
								{
									type: 'date',
									required: true,
									message: '请选择时间',
									trigger: 'blur'
								}
							],
							
							fback: [{

								required: true,
								message: '请选择负责人',
								trigger: 'blur'
							}]
						}
					}
				},
				methods: {
                       submitForm(formName) {
						//验证方法
						this.$refs[formName].validate((valid) => {
							if(valid) {
								var ax = null;
								if (JSON.stringify(${addsolution}) == '{}') {
									 alert(ax+"ssss");
									ax = "/addsolution";
								}else{
									 alert(ax+"ssss");
								      ax = "/updatesolution";
								}
								//post提交数据
								axios.post(ax,
									//参数转换
									Qs.stringify(this.form, {
										arrayFormat: 'brackets'
									})
								).then(function(v) {
									window.parent.location.replace("/solution");
								}).catch(function(e) {
									alert("出错了"+e);
								});
							} else {
								console.log('error submit!!');
								return false;
							}
						});
					},
					resetForm(formName) {
						this.$refs[formName].resetFields();
					},
					querySearch: function(queryString, cb) {
						 var allarr = this.form.restaurants;
				    	 // console.info(allarr);
				    	 
							var newsarr = [];
							allarr.forEach(v => {
								 var strs =v.cus_name+"";
								 if (strs.indexOf(queryString) >= 0) {
										newsarr.push(v);
										console.info(strs);
										console.info(v);
									} 
							})
							// 调用 callback 返回建议列表的数据
							cb(newsarr);
					},
					handleSelect: function(it) {
						console.log(it.cus_name);
						this.form.custname=it.cus_name;
						this.form.custid=it.cus_id;
						 var iosnn=this;
					        
							//post提交数据
							axios.post("/onSale/findByCoustomerName",
								//参数转换
								Qs.stringify(this.form, {
									arrayFormat: 'brackets'
								})		
							).then(function(v) {
								iosnn.customertoman = v.data.custo;
								iosnn.opport = v.data.opp;
								console.info(v.data); 
							}).catch(function(e) {
								alert("出错了"+e);
							});
					},
					cha: function(it) {
						this.form.oppid = it;
						alert(this.form.oppid);
					}

				},
				mounted() {
					//console.log(dic); 
					var mas=${mansitem};
					this.form.restaurants = mas;
					//console.log(this.form.restaurants); 
					var solution = ${addsolution};
						if(solution){
							var news = ${addsolution};
						}
						console.log(solution);
					 this.form.theme = news.sol_name;  
					 this.form.custname = news.cus_name;
					 this.form.custid = news.cus_id;
					 this.form.solid = news.sol_id
					 this.form.oppid = news.opp_id;
					 this.form.sdate = news.sol_submission;
					 this.form.cont = news.sol_content;
					 this.form.fback = news.sol_feedback;

				} 
			});
		</script>
	</body>

</html>
